package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.xgkj.diyiketang.activity.data.PhotoAdapter;
import com.xgkj.diyiketang.data.PhotoInfo;
import com.xgkj.diyiketang.task.ChaKanXiangCeTask;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.AnchorListAdapter;
import com.xgkj.diyiketang.activity.data.VideoAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.SellerInfo;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AnchorListTask;
import com.xgkj.diyiketang.task.GetSellerVideoListTask;
import com.xgkj.diyiketang.task.GetUserInfoTask;
import com.xgkj.diyiketang.task.ShangJiaDetailTask;
import com.xgkj.diyiketang.util.DialogUtil;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.MyScrollView;
import com.xgkj.diyiketang.weidget.NoScrollGridView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2016/3/20.
 */
public class UserDetailActivity extends BaseActivity  implements MyScrollView.OnScrollListener,View.OnClickListener{
    private Context mContext;
    private String userid;
    private EatShowUserInfo sinfo;
    private MyScrollView mScrollView;
    private int userdetailTaskID = -1;
    private int currentPage = 1;
    private int size = 24;
    private TextView mTitle;
    private ImageView mSellerLogo;
    private TextView mSellerName;
   // private TextView mHisGuanZhu;
    private TextView mRenjun;

    private TextView back ;

    private NoScrollGridView mGridView;
    private AnchorListAdapter mAdapter;

    private NoScrollGridView photoGridView;
    private PhotoAdapter photeAdapter;
    private int anchorListTaskId = -1;
    private CheckBox mGuanZhuBtn;
    private CheckBox mPhotoBtn;
   // private LinearLayout mGuanzhuLayout;
    private LinearLayout mPhotoLayout;
    private int xiangceListTaskId = -1;
    private int currentPage_p =1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_detail);
        mContext = this;
       initView();
    }

    private void initView() {
        Intent intent = getIntent();
        userid = intent.getStringExtra("userId");
        mScrollView = (MyScrollView) findViewById(R.id.user_scrollView);
        mScrollView.setOnScrollListener(this);
        mGridView = (NoScrollGridView) findViewById(R.id.user_focus_list);
        mAdapter = new AnchorListAdapter(mContext);
        mGridView.setAdapter(mAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //PalLog.e("位置",position+"");

                if (position < 0 || position >= mAdapter.getCount()) return;
                EatShowUserInfo info = (EatShowUserInfo) mAdapter.getItem(position);
                startActivity(AnchorActivity.getIntent(mContext, info.getUserId()));
            }
        });

        photoGridView = (NoScrollGridView) findViewById(R.id.user_photo_list);
        photeAdapter = new PhotoAdapter(mContext,photoGridView);
        photoGridView.setAdapter(photeAdapter);

        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
       // mHisGuanZhu = (TextView) findViewById(R.id.hisguanzhu);
        mTitle = (TextView) findViewById(R.id.user_name_title);
        mSellerLogo = (ImageView) findViewById(R.id.head_img);
        mSellerLogo.setFocusable(true);
        mSellerLogo.setFocusableInTouchMode(true);
        mSellerLogo.requestFocus();
        mSellerName = (TextView) findViewById(R.id.nickname);
        mRenjun = (TextView) findViewById(R.id.person_signature);
        mGuanZhuBtn = (CheckBox) findViewById(R.id.guanzhu_btn);
        mGuanZhuBtn.setOnClickListener(this);
        mGuanZhuBtn.setChecked(true);

        mPhotoBtn = (CheckBox) findViewById(R.id.photo_btn);
        mPhotoBtn.setOnClickListener(this);
        mPhotoBtn.setChecked(false);
        //mGuanzhuLayout = (LinearLayout) findViewById(R.id.guanzhu_layout);
        mPhotoLayout = (LinearLayout) findViewById(R.id.photo_layout);
       // mGuanzhuLayout.setVisibility(View.VISIBLE);
        mPhotoLayout.setVisibility(View.GONE);

        getData();
    }

    private void getVideoList(){
        AnchorListTask myAttentionListTask = new AnchorListTask(size,currentPage,userid,"","","3",null);
        myAttentionListTask.addListener(sellerDetailTask);
        anchorListTaskId = getEatShowApp().getTaskManager().addTask(myAttentionListTask);

    }

    private void getMyPhotos(){
        ChaKanXiangCeTask task = new ChaKanXiangCeTask(size,currentPage_p,userid);
        task.addListener(sellerDetailTask);
        xiangceListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    @Override
    public void onScroll() {
        getData();
    }

    private void getData(){
        if(currentPage ==0){
           // Toast.makeText(mContext, R.string.no_more, Toast.LENGTH_SHORT).show();
            return ;
        }
        if(userdetailTaskID != -1) {
            return;
        }
        GetUserInfoTask task = new GetUserInfoTask(userid);
        task.addListener(sellerDetailTask);
        userdetailTaskID = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener sellerDetailTask = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {

            if(taskId == userdetailTaskID){
                sinfo = (EatShowUserInfo) data;
                if(sinfo.getNickName()!=null){
                    try {
                        mTitle.setText(URLDecoder.decode(sinfo.getNickName(),"utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                String anchorImgUrl = sinfo.getUserLogo();
                if(anchorImgUrl != null && anchorImgUrl.length()>0){
                    ImageCache.getInstance(mContext).loadBitmapToView(mSellerLogo,anchorImgUrl, null);
                }
                if(sinfo.getUserSign()!=null){
                    try {
                        mRenjun.setText(URLDecoder.decode(sinfo.getUserSign(),"utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                if(sinfo.getNickName()!=null){
                    try {
                        mSellerName.setText(URLDecoder.decode(sinfo.getNickName(),"utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }



                getVideoList();
            }else if(taskId == anchorListTaskId){
                anchorListTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();

                }
                List<EatShowUserInfo> attentionList = new ArrayList<EatShowUserInfo>();
                if(data != null){
                    attentionList = (List<EatShowUserInfo>) data;
                }

                if(attentionList != null && attentionList.size()>0){
                    mAdapter.updateData(attentionList);
                  // mHisGuanZhu.setText(mContext.getString(R.string.tadeguanzhu) + "   (" + attentionList.size() + "个" + ")");
                }

                currentPage = result;

            }else if(taskId == xiangceListTaskId){
                xiangceListTaskId = -1;
                if (data != null){
                    List<PhotoInfo> list = (List<PhotoInfo>) data;
                    if (currentPage_p ==1){
                        photeAdapter.clearData();
                    }
                    photeAdapter.updateData(list);
                   // photoGridView.onRefreshComplete();
                    currentPage_p = result;

                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == userdetailTaskID){
                userdetailTaskID = -1;
            }else if (taskId == anchorListTaskId){
                anchorListTaskId = -1;
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    public void onClick(View v) {
            switch (v.getId()){
                case R.id.guanzhu_btn:
                    mGuanZhuBtn.setChecked(true);
                    mPhotoBtn.setChecked(false);
                    mPhotoLayout.setVisibility(View.GONE);
                    mGridView.setVisibility(View.VISIBLE);
                    if(mAdapter!=null&&mAdapter.getCount()==0){
                        currentPage = 1;
                        getVideoList();
                    }
                    break;
                case R.id.photo_btn:
                    mPhotoBtn.setChecked(true);
                    mGuanZhuBtn.setChecked(false);
                    mPhotoLayout.setVisibility(View.VISIBLE);
//                    List<PhotoInfo> photoList = new ArrayList<PhotoInfo>();
//                    photeAdapter.updateData(photoList);
                    mGridView.setVisibility(View.GONE);
                    if(photeAdapter!=null&&photeAdapter.getCount()==0){
                        currentPage_p = 1;
                       getMyPhotos();
                    }
                    break;
            }
    }
}
